package org.zsqx.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class FileTools {

	public static List<FileBean> getFiles(String path, long timeForm,
			long timeTo)throws Exception {
		File f = new File(path);
		return tree(f, 1, timeForm, timeTo);
	}

	private static List<FileBean> tree(File f, int level, long timeForm,
			long timeTo) throws Exception {
		List<FileBean> files = new ArrayList<FileBean>();
		File[] childs = f.listFiles();
		for (int i = 0; i < childs.length; i++) {
			long timeFile = childs[i].lastModified();
			if (timeFile >= timeForm && timeFile <= timeTo) {
				FileBean file = new FileBean();
				file.setName(childs[i].getName());
				file.setCtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.format(timeFile));
				files.add(file);
			}
			if (childs[i].isDirectory()) {
				tree(childs[i], level + 1, timeForm, timeTo);
			}
		}
		return files;
	}

	public static void main(String[] args) throws Exception {
		//\\172.22.42.249\j\大容量图片资料\SurfaceTemp\2008\08\05\08080511.jpg
		String shareDirectoryName = "j\\大容量图片资料\\SurfaceTemp\\2008\\08\\05";
		String host = "172.22.42.249";// 需要访问的主机host
		String username = "aa";// 访问用户
		String password = "aa";// 密码
		String path = "\\\\" + host + "\\" + shareDirectoryName;// 访问路径
		if (username != null && username.trim().length() > 0) {
			Runtime runtime = Runtime.getRuntime();
			String command = "net use " + path + " " + " /delete";
			Process p = runtime.exec(command);
			command = "net use " + path + " " + password + " /" + "user:"
					+ username;
			p = runtime.exec(command);

			InputStream stderr = p.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = "";
			StringBuffer errorsb = new StringBuffer();
			while (line != null) {
				line = br.readLine();
				if (line != null)
					errorsb.append(line);
			}
			if (errorsb != null && errorsb.toString().trim().length() > 0) {
				//System.out.println(errorsb.toString());
				throw new Exception(errorsb.toString());
			}
			
			File dir = new File(path);
			if (dir.exists() && dir.isDirectory()) {
				File[] tmp = dir.listFiles();
				for (File file : tmp) {
					//System.out.println(file.getName());
				}
			} else {
				if (dir.exists()) {
					//System.out.println(dir.getName());
				}
			}
		}

	}
}